home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / sunbird / js / calWeekTitleService.js < prev   
Encoding:
JavaScript  |  2007-05-23  |  5.3 KB  |  119 lines

  1. /* -*- Mode: idl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is Oracle Corporation code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  *  Michiel van Leeuwen <mvl@exedo.nl>
  19.  * Portions created by the Initial Developer are Copyright (C) 2006
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. function calWeekTitleService() {
  39. }
  40.  
  41. calWeekTitleService.prototype.QueryInterface =
  42. function QueryInterface(aIID) {
  43.     if (!aIID.equals(Components.interfaces.nsISupports) &&
  44.         !aIID.equals(Components.interfaces.calIWeekTitleService)) {
  45.         throw Components.results.NS_ERROR_NO_INTERFACE;
  46.     }
  47.  
  48.     return this;
  49. };
  50.  
  51. calWeekTitleService.prototype.getWeekTitle =
  52. function getWeekTitle(aDateTime) {
  53.     /**
  54.      * This implementation is based on the ISO 8601 standard.  
  55.      * ISO 8601 defines week one as the first week with at least 4
  56.      * days, and defines Monday as the first day of the week.
  57.      * Equivalently, the week one is the week with the first Thursday.
  58.      * 
  59.      * This implementation uses the second definition, because it
  60.      * enables the user to set a different start-day of the week
  61.      * (Sunday instead of Monday is a common setting).  If the first
  62.      * definition was used, all week-numbers could be off by one
  63.      * depending on the week start day.  (For example, if weeks start
  64.      * on Sunday, a year that starts on Thursday has only 3 days
  65.      * [Thu-Sat] in that week, so it would be part of the last week of
  66.      * the previous year, but if weeks start on Monday, the year would
  67.      * have four days [Thu-Sun] in that week, so it would be counted
  68.      * as week 1.)
  69.      */
  70.  
  71.     // The week number is the number of days since the start of week 1,
  72.     // divided by 7 and rounded up. Week 1 is the week containing the first
  73.     // Thursday of the year.
  74.     // Thus, the week number of any day is the same as the number of days
  75.     // between the Thursday of that week and the Thursday of week 1, divided
  76.     // by 7 and rounded up. (This takes care of days at end/start of a year
  77.     // which may be part of first/last week in the other year.)
  78.     // The Thursday of a week is the Thursday that follows the first day
  79.     // of the week.
  80.     // The week number of a day is the same as the week number of the first
  81.     // day of the week. (This takes care of days near the start of the year,
  82.     // which may be part of the week counted in the previous year.) So we
  83.     // need the startWeekday.
  84.     const SUNDAY = 0;
  85.     var startWeekday = SUNDAY; // default to monday per ISO8601 standard.
  86.     try {     
  87.         var prefBranch = Components.classes["@mozilla.org/preferences-service;1"]
  88.                                    .getService(Components.interfaces.nsIPrefBranch);
  89.         startWeekday = prefBranch.getIntPref("calendar.week.start");
  90.     } catch (e) {}
  91.  
  92.     // The number of days since the start of the week.
  93.     // Notice that the result of the substraction might be negative.
  94.     // We correct for that by adding 7, and then using the remainder operator.
  95.     var sinceStartOfWeek = (aDateTime.weekday - startWeekday + 7) % 7; 
  96.  
  97.     // The number of days to Thursday is the difference between Thursday
  98.     // and the start-day of the week (again corrected for negative values).
  99.     const THURSDAY = 4;
  100.     var startToThursday = (THURSDAY - startWeekday + 7) % 7;
  101.  
  102.     // The yearday number of the Thursday this week.
  103.     var thisWeeksThursday = aDateTime.yearday - sinceStartOfWeek + startToThursday;
  104.  
  105.     // For the first few days of the year, we might still be in week 52 or 53.
  106.     if (thisWeeksThursday < 1) {
  107.         var lastYearDate = aDateTime.clone();
  108.         lastYearDate.year -= 1;
  109.         thisWeeksThursday += lastYearDate.endOfYear.yearday;
  110.     }
  111.  
  112.     // For the last few days of the year, we might already be in week 1. 
  113.     if (thisWeeksThursday > aDateTime.endOfYear.yearday)
  114.         thisWeeksThursday -= aDateTime.endOfYear.yearday;
  115.  
  116.     var weekNumber = Math.ceil(thisWeeksThursday/7);
  117.     return weekNumber;
  118. };
  119.